package org.springframework.core.type.classreading;

import org.springframework.asm.SpringAsmInfo;
import org.springframework.asm.signature.SignatureVisitor;

public class ServiceSignatureReadingVisitor extends SignatureVisitor{
	private final ServiceMethodMetadataReadingVisitor methodVisitor;
	public ServiceSignatureReadingVisitor(ServiceMethodMetadataReadingVisitor methodVisitor) {
		super(SpringAsmInfo.ASM_VERSION);
		this.methodVisitor = methodVisitor;
	}
	private final SignatureVisitor parameterVisitor = new SignatureVisitor(api){

		@Override
		public void visitClassType(String name) {
			methodVisitor.parameters[parameter].typeVariables.add(name);
		}
		
		@Override
		public void visitBaseType(char name) {
			methodVisitor.parameters[parameter].typeVariables.add(String.valueOf(name));
		}
		
		@Override
		public SignatureVisitor visitArrayType() {
			methodVisitor.parameters[parameter].isArrayType = true;
			return this;
		}
	};
	private final SignatureVisitor returnVisitor = new SignatureVisitor(api){

		@Override
		public void visitClassType(String name) {
			methodVisitor.typeVariables.add(name);
		}
		
		@Override
		public void visitBaseType(char name) {
			methodVisitor.typeVariables.add(String.valueOf(name));
		}
		@Override
		public SignatureVisitor visitArrayType() {
			methodVisitor.isArrayReturnType = true;
			return this;
		}
	};
	
	@Override
	public SignatureVisitor visitReturnType() {
		return returnVisitor;
	}
	int parameter = -1;
	@Override
	public SignatureVisitor visitParameterType() {
		parameter++;
		return parameterVisitor;
	}
}
